package com.ruoyi.energy.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.energy.domain.TcInoutLog;
import com.ruoyi.energy.domain.vo.ParkCarEnterVO;
import com.ruoyi.energy.domain.vo.ParkCarOutVO;
import com.ruoyi.energy.service.IParkingHandlerService;
import com.ruoyi.energy.service.ITcInoutLogService;
import com.ruoyi.energy.utils.HttpHelper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@Api(tags = " B区停车系统接口")
@RestController
//@Controller
@RequestMapping("/interface")
@Slf4j
public class EPParkInterfaceController extends BaseController {


    @Autowired
    private IParkingHandlerService handlerService;

    @Autowired
    private ITcInoutLogService logservice;

    @PostMapping("/handleMessage")
    //public AjaxResult handlePushMessage(@RequestBody String request){ HttpServletRequest request
     public AjaxResult handlePushMessage(HttpServletRequest request1){
        String request = HttpHelper.getBodyString(request1);
        log.info("EPParkInterfaceController handlePushMessage " + request );
        JSONObject js = JSONUtil.parseObj(request);
        String fnName = js.get("fnName").toString();
        TcInoutLog log = new TcInoutLog();
        log.setInOut(fnName);
        log.setContent(JSONUtil.toJsonStr(request));

        if (("outParkEnd").equals(fnName)){
            //出场回调
            ParkCarOutVO enterVO = JSONUtil.toBean(js, ParkCarOutVO.class);
            if (enterVO.getData().size() > 0){
                log.setPlateId(enterVO.getData().get(0).getPlateId());
            }
            handlerService.handlerOutParkMessage(enterVO);
        }else if(("inParkEnd").equals(fnName)){
            //入场回调
            ParkCarEnterVO enterVO = JSONUtil.toBean(js, ParkCarEnterVO.class);
            if (enterVO.getData().size() > 0){
                log.setPlateId(enterVO.getData().get(0).getPlateId());
            }
            handlerService.handlerInParkMessage(enterVO);
        }
        logservice.insertTcInoutLog(log);
        return AjaxResult.success();
    }



}
